//
//  ChagePersonCon.m
//  ShopDemo
//
//  Created by Sunshine on 2018/7/27.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "ChagePersonCon.h"

@interface ChagePersonCon ()

@property(nonatomic,strong)NSDictionary *persondic;

@property(nonatomic,strong)UITextField *usfield;
@property(nonatomic,strong)UITextField *agefield;
@property(nonatomic,strong)UITextField *emfield;

@end

@implementation ChagePersonCon

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHeaderViewMethod];
    NSString *strurl = [NSString stringWithFormat:@"/dashop/happuser/app/list.do?USERNAME=%@",[UserDefault objectForKey:LogUserName]];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
    [self PersonMessageMethodWithUrl:endstr];
}

-(void)PersonMessageMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        self.persondic = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            self.usfield.text = self.persondic[@"NAME"];
            if ([[self.persondic allKeys]containsObject:@"YEARS"]) {
                self.agefield.text = [NSString stringWithFormat:@"%@",self.persondic[@"YEARS"]];
            }
            if ([[self.persondic allKeys]containsObject:@"EMAIL"]){
                self.emfield.text = [NSString stringWithFormat:@"%@",self.persondic[@"EMAIL"]];
            }
        }];
    } failBlock:^(NSError *error) {
        
    }];
    
}


-(void)creatHeaderViewMethod{
    self.view.backgroundColor = LineBackColor;
    UIView *backview = [UIView new];
    backview.backgroundColor = [UIColor whiteColor];
    backview.layer.masksToBounds = YES;
    backview.layer.cornerRadius = 5*kJLXWidthScale;
    [self.view addSubview:backview];
    backview.sd_layout
    .topSpaceToView(self.view, 20*kJLXWidthScale)
    .leftSpaceToView(self.view, 20*kJLXWidthScale)
    .rightSpaceToView(self.view, 20*kJLXWidthScale)
    .heightIs(150*kJLXWidthScale);
    for (int i = 0; i < 3; i ++) {
        UITextField *field = [UITextField new];
        field.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
        if (i == 0) {
            [backview addSubview:field];
            self.usfield = field;
            field.placeholder = @"用户名";
        }else if (i == 1){
            [backview addSubview:field];
            self.agefield = field;
            field.placeholder = @"年龄";
        }else if (i == 2){
            [backview addSubview:field];
            self.emfield = field;
            field.placeholder = @"邮箱";
        }
        field.sd_layout
        .topSpaceToView(backview, i*50*kJLXWidthScale)
        .rightSpaceToView(backview, 10*kJLXWidthScale)
        .leftSpaceToView(backview, 10*kJLXWidthScale)
        .heightIs(50*kJLXWidthScale);
    }
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 8*kJLXWidthScale;
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"提交" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(saveButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    btn.sd_layout
    .topSpaceToView(backview, 50*kJLXWidthScale)
    .leftSpaceToView(self.view, 20*kJLXWidthScale)
    .rightSpaceToView(self.view, 20*kJLXWidthScale)
    .heightIs(45*kJLXWidthScale);
}


-(void)saveButtonAction:(UIButton *)sender{
    if (self.usfield.text.length == 0) {
        [self alerWithMessage:@"用户不能为空"];
    }else if (self.agefield.text.length == 0){
        [self alerWithMessage:@"年龄不能为空"];
    }else if (self.emfield.text.length == 0){
         [self alerWithMessage:@"邮箱不能为空"];
    }else{
        /*
         USERNAME 用户账号
         PASSWORD 密码
         SEX 性别（男：male，女：female）
         YEARS 年龄
         EMAIL 邮箱
         PHONE 手机号
         NAME 呢称
         USER_ID 主键ID
         */
        NSString *strurl = [NSString stringWithFormat:@"/dashop/happuser/app/save.do?USERNAME=%@&PASSWORD=%@&SEX=%@&YEARS=%@&EMAIL=%@&PHONE=%@&NAME=%@&USER_ID=%@",self.persondic[@"USERNAME"],[UserDefault objectForKey:LogPassWord],self.persondic[@"SEX"],self.agefield.text,self.emfield.text,self.persondic[@"PHONE"],self.usfield.text,self.persondic[@"USER_ID"]];
        NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
        NSString  *newUrlString = [endstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [self changePassMethodWithUrl:newUrlString];
    }
    
}


-(void)changePassMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        if ([dic[@"result"] integerValue] == 1) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self alerWithMessage:dic[@"MESSAGE"]];
        }
    } failBlock:^(NSError *error) {
        
    }];
    
}


//封装提示框方法
-(void)alerWithMessage:(NSString *)msg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        //延迟1秒自动消失
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [alert dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}

@end
